var path;
var shopId;
var doing;
$(document).ready(function(){
	path = $("#path").val();
	shopId = $("#shopId").val();
	doing = false;
	
	// choose address
	//$(".addressComponent").live("click",chooseAddress);
	
	// choose cash coupon
	$(".cash-coupon").live("click",chooseCashCoupon);
	
	// choose logistics coupon
	$(".logistics-coupon").live("click",chooseLogisticsCoupon);
	
	// choose payment type
	$(".payment").live("click",choosePayment);
	
	// see item detail
	$(".box-item-detail").live("click",findItems)
	
	// find available coupon number
	findCouponNumber();
	
	// submit order
	//$(".submit-btn").live("click",submitOrder);
});

function findItems(){
	window.location.href=path+"/merchandise/settlementItems/"+shopId;
}
function checkNameUnique(){
	var recepientName = $("#recepientName").html();
	var identityCardName = $("#identityCardName").html();
	if(recepientName!=null&&identityCardName!=null){
		recepientName = recepientName.trim();
		identityCardName = identityCardName.trim();
		if(identityCardName!=recepientName){
			$.dialog({
		        content : '【收货人】和【身份证姓名】必须一致',
		        title : '温馨提示',
		        lock : true,
		        time: 2000
		    });
			doing = false;
		}
	}
}
function submitOrder(){
	if(doing)return;
	
	doing  = true;
	
	checkNameUnique();
	if(!doing)return;
	
	var payType = $(".payment:checked");
	if(payType.length == 0){
		
		$.dialog({
	        content : '请选择支付方式',
	        title : '温馨提示',
	        lock : true,
	        time: 2000
	    });

		doing = false;
		
		return;
	}
	
	$.ajax({
        type: "post",
        dataType: "json",
        async:true,
        url:path+"/order/submit/",
        success: function (result) {
        	
        	var code = result.code;
        	var msg = result.msg;
        	
        	$.dialog({
    			content : result.msg,
    			title : '温馨提示',
    			lock : true,
    			ok : function() {
    				if('YYK-10000' == code){
    					var orderId = result.orders.orderId;
    					redirectUrl = path+"/payment/select/"+orderId+"/"+payType.val();
    					window.location.href = redirectUrl;
    				}
    			}
    		});
        	doing = false;
        },error: function(){
        	$.dialog({
    			content : '提交订单失败',
    			title : '温馨提示',
    			lock : true,
    			ok : function() {
    			}
    		});
        	doing = false;
        }
	 });
	
}

function chooseAddress(){
	window.location.href=path+"/address/find";
}
function findIDCard(){
	var recepientName = $("#recepientName").html();
	if(recepientName==null||recepientName==""){
		$.dialog({
			content : '请先填写收货信息',
			title : '温馨提示',
			lock : true,
			time: 2000
		});
		return;
	}
	window.location.href=path+"/identitycard/view/0/"+shopId;
}


function chooseCashCoupon(){
	if($(this).hasClass("noneCoupon")){
		return;
	}
	chooseCoupon(0);
}

function chooseLogisticsCoupon(){
	if($(this).hasClass("noneCoupon")){
		return;
	}
	chooseCoupon(1);
}

function chooseCoupon(couponType){
	if(couponType != 0 && couponType != 1){
		return;
	}
	
	window.location.href=path+"/coupon/find/"+couponType+"/"+shopId;
}

function choosePayment(){
	var payType = $(".payment:checked").val();
	$.ajax({
        type: "post",
        dataType: "text",
        async:true,
        url:path+"/payment/choose/"+payType,
        success: function (result) {
        }
	 });
}

function findCouponNumber(){
	var shopId = $("#shopId").val();
	$.ajax({
        type: "post",
        dataType: "text",
        async:true,
        url:path+"/coupon/qty/"+shopId,
        success: function (result) {
        	setCouponQty(result);
        }
	 });
}

function setCouponQty(result){
	var cashCouponQty = 0;
	var logisticsCouponQty = 0;
	if(!isEmpty(result)){
		result = JSON.parse(result);
		cashCouponQty = result[0];
		logisticsCouponQty = result[1];
	}
	
	$("#cash-coupon").find("#tranCouponNum").text(cashCouponQty);
	$("#logistics-coupon").find("#tranCouponNum").text(logisticsCouponQty);
	
	if(Number(cashCouponQty) == 0){
		$("#cash-coupon").parent().addClass("noneCoupon");
	}
	
	if(Number(logisticsCouponQty) == 0){
		$("#logistics-coupon").parent().addClass("noneCoupon");
	}
}